package com.demo.controller;

import com.demo.config.RedissonSpringDataConfig;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/api/redisson")
public class RedissonController {

    @GetMapping("/getRLock")
    public boolean getRLock() throws InterruptedException {
        RLock myLock = RedissonSpringDataConfig.redissonClient.getLock("myLock");
        boolean tryLock = myLock.tryLock(10, 30, TimeUnit.SECONDS);
        log.info("tryLock:" + tryLock);
        // hgetall myLock 命令可以看到myLock对象在redis内结构是一个字典（key:myLock,field:UUID+threadId,value:重入次数)
        return tryLock;
    }

}
